#include <iostream>
#include <list>
#include <string>
#include <algorithm>
#include <vector>
#include <random>

using namespace std;
class User
{
public:
    string _name;
    int _age;
public:
    User(string name, int age)
    :_name(name)
    ,_age(age) 
    {}
    ~User() {}

    // 方案一
    // 重载一个< 运算符
    // bool operator< (const User &other){
    //     bool r =  _age < other._age;
    //     if(_age == other._age)
    //         r = _name < other._name;
    //     return r;
    // }
};
// 方案二 定义函数
bool user_order_by_name(const User &lhs, const User &rhs)
{
    return lhs._name < rhs._name;
}

int main(int argc, char const *argv[])
{
    vector<int> l1 = {100, 500, 300};
    list<string> l2;
    vector<User> l3;
    User u1("bob", 20);
    User u2("link", 20);
    User u3("jack", 80);
    User u4("king", 100);
    l3.push_back(u1);
    l3.push_back(u2);
    l3.push_back(u3);
    l3.push_back(u4);
    // 第三个参数定义的排序规则，替代默认的排序规则
    // sort(l3.begin(), l3.end(), user_order_by_name);
    sort(l3.begin(), l3.end(), 
            [](User &u1, User &u2){return u1._age > u2._age;});

    // 查找
    // find()
    // 乱序
    // shuffle()
    // 翻转
    // reverse()

    // 方案三 lambda表达式
    // 匿名函数,通过参数传递一个回调函数
    // 函数式编程中的概念
    // auto fn = [](int a){cout << a << endl;};
    // fn(9);
    for (auto &&i : l3)
    {
        cout << i._name << "," << i._age << endl;
    }

    sort(l1.begin(), l1.end());
    for (auto &&i : l1)
    {
        cout << i << ",";
    }
    cout << endl;

    // reverse(l1.begin(), l1.end());
    default_random_engine rde(time(NULL));
    shuffle(l1.begin(), l1.end(), rde);
    for (auto &&i : l1)
    {
        cout << i << ",";
    }
    cout << endl;


    cout << l1.size() << "," << l1.max_size() << endl;
    // cout << l1[0];   list不支持下标访问

    // auto it = l1.begin();
    // cout << *it << endl;
    
    // assign 将值赋给容器，5为容器的新大小，要初始化容器的值
    l1.assign(5, 1);
    l1.push_back(555);
    // l1.push_front(666);

    auto it2 = l1.end();
    l1.erase(l1.begin());
    
    // l1.reverse();
    // 范围循环遍历
    for(auto i = l1.begin(); i != l1.end(); i++)
    {
        cout << *i << ", ";
    }
    cout << endl;

    return 0;
}
